' Calculate gross pay PRINT "The pay is", 16 * 7.25 END
If you run this program it writes:
The pay is 116
So far we have seen the QBasic commands for adding two numbers (+) and for multiplying two numbers (*). The + and * are called arithmetic operators. Here is a list of more of them:
operator |
meaning | example | in words |
---|---|---|---|
^ |
power | 3^2 | 3 to the power 2 |
- |
negation | -23 | negative 23 |
* |
multiply | 1.5 * 8 | 1.5 times 8 |
/ |
divide | 12 / 4 | 12 divided by 4 |
+ |
addition | 4.2 + 3 | 4.2 plus 3 |
- |
subtraction | 9 - 2 | 9 minus 2 |
Here is a program that calculates the number of miles per gallon for a car that has burned 10 gallons of gas and gone 245.4 miles:
' Calculate miles per gallon ' 245.4 miles with 10 gallons of gas ' PRINT "MPG is", 245.4 / 10 END
It is important to get the two numbers in the correct order. The program is correct because it divides the number of miles, 245.4, by the number of gallons, 10. The program prints its output to the monitor:
MPG is 24.54
This program has three comment lines. This is fine; comments are ignored by the computer. You can have many of them. The third comment line has nothing on it except for the apostrophe ( ' ) that makes it a comment. This is fine. You can use a blank line if you want.
Write (on paper, in your head, or on a computer) a program to answer the following problem:
A bird watcher bought 25 pounds of bird food for an outdoor bird feeder. The birds ate all the food in 15 days. How many pounds of bird food per day did the birds eat?